package com.dpj.seckill.controller;

import com.dpj.seckill.pojo.GoodsVo;
import com.dpj.seckill.pojo.TUser;
import com.dpj.seckill.service.GoodsService;
import com.dpj.seckill.service.TUserService;
import com.dpj.seckill.vo.DetailsVo;
import com.dpj.seckill.vo.RespBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/goods")
public class GoodsController {


    @Autowired
    private TUserService userService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;



    /** windows优化前qps:242
     *  linux下优化前qps:330
     * 跳转到商品页
     * @param model
     * @param user
     * @return
     */


    //跳转到商品页
    @RequestMapping(value = "/toList",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toLogin(Model model, TUser user,HttpServletRequest request,HttpServletResponse response) {
//        if(StringUtils.isEmpty(ticket)){
//            return "login";
//        }
        //TUser user=(TUser)session.getAttribute(ticket);
        //    userService.getUserByCookie(ticket,request,response);
//        if(null==user){
//            return "login";
//        }
        //redis中获取页面，如果不为空，直接返回界面
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html = (String) valueOperations.get("goodsList");
        if(!StringUtils.isEmpty(html)){
            return html;
        }
        model.addAttribute("user", user);
        model.addAttribute("goodsList", goodsService.findGoodsVo());
        //return "goodsList";
        //如果为空，手动渲染，存入redis并返回
        WebContext webContext=new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
        html=thymeleafViewResolver.getTemplateEngine().process("goodsList",webContext);
        if(!StringUtils.isEmpty(html)){
            valueOperations.set("goodsList",html,60, TimeUnit.SECONDS);
        }
        return html;
    }



    @RequestMapping(value = "toDetail/{id}" )
    public String toDetali(Model model, TUser user, @PathVariable Long id) {


        model.addAttribute("user", user);
        GoodsVo goodsVo = goodsService.findGoodById(id);
        model.addAttribute("goodVo", goodsVo);

         return "goodsDetail";
    }

//    @RequestMapping(value = "toDetail1/{id}", produces = "text/html;charset=utf-8")
//    @ResponseBody
//    public String toDetail(Model model, TUser user, @PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {
//        ValueOperations valueOperations = redisTemplate.opsForValue();
//        String html = (String) valueOperations.get("goodsDetail:" + id);
//        if(!StringUtils.isEmpty(html)){
//            return html;
//        }
//        model.addAttribute("user", user);
//        GoodsVo goodsVo = goodsService.findGoodsById(id);
//
//        Date startDate = goodsVo.getStartDate();
//        Date endDate=goodsVo.getEndDate();
//        Date nowDate=new Date();
//        //秒杀状态
//        int seckillStatus=0;
//
//        // 开始剩余时间
//        int remainSeconds;
//        //秒杀未开始
//        if(nowDate.before(startDate)){
//         remainSeconds= (int) ((startDate.getTime()-nowDate.getTime())/1000);
//
//            //秒杀已结束
//        }else if(nowDate.after(endDate)){
//            seckillStatus=2;
//            remainSeconds=-1;
//            //秒杀中
//        }else{
//            seckillStatus=1;
//            remainSeconds=-1;
//        }
//
//
//        model.addAttribute("goods", goodsVo);
//        model.addAttribute("seckillStatus",seckillStatus);
////        model.addAttribute(" remainSeconds1", 2);
//        model.addAttribute("remainSecond",remainSeconds);
//
//        WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
//        html = thymeleafViewResolver.getTemplateEngine().process("goodsDetail", webContext);
//        if(!StringUtils.isEmpty(html)){
//            valueOperations.set("goodsDetail:"+id,html,60,TimeUnit.SECONDS);
//
//        }
//        return html;
//
//
//       // return "goodsDetail";
//
//
//    }


    @RequestMapping( "toDetail1/{id}")
    @ResponseBody
    public RespBean toDetail(Model model, TUser user, @PathVariable Long id) {

        GoodsVo goodsVo = goodsService.findGoodsById(id);
        Date startDate = goodsVo.getStartDate();
        Date endDate=goodsVo.getEndDate();
        Date nowDate=new Date();
        //秒杀状态
        int seckillStatus=0;
        // 开始剩余时间
        int remainSeconds;
        //秒杀未开始
        if(nowDate.before(startDate)){
            remainSeconds= (int) ((startDate.getTime()-nowDate.getTime())/1000);

            //秒杀已结束
        }else if(nowDate.after(endDate)){
            seckillStatus=2;
            remainSeconds=-1;
            //秒杀中
        }else{
            seckillStatus=1;
            remainSeconds=0;
        }

        DetailsVo detailsVo=new DetailsVo();
        detailsVo.setGoodsVo(goodsVo);
        detailsVo.setRemainSeconds(remainSeconds);
        detailsVo.setUser(user);
        detailsVo.setSecKillStatus(seckillStatus);
        return RespBean.success(detailsVo);








        // return "goodsDetail";


    }

}
